/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package homebudget;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

/**
 *
 * @author Krit
 */
public class DataBase {

    public LinkedList<Entry> expenses = new LinkedList<Entry>();
    public LinkedList<Entry> contributions = new LinkedList<Entry>();
    NumberFormat nf = NumberFormat.getInstance();
    public static final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, ''yy");
    
    public DataBase() {         
    }

    public void addExpensesList(LinkedList<Entry> lista){
        expenses.clear();
        for (Entry entry : lista) {
            expenses.add(entry);
        }
    }

    public void addContributionsList(LinkedList<Entry> lista){
        contributions.clear();
        for(Entry entry : lista){
            contributions.add(entry);
        }
    }

    public String[] getExpensesArray() {
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        addExpensesList(baza.pobierzWydatki(HomeBudgetApp.getIdbudzetu()));
        baza.close();

        nf.setMinimumFractionDigits(2);
        LinkedList<String> nazwyWydatkow = new LinkedList<String>();
        LinkedList<String> tytulWydatku = new LinkedList<String>();
        for(Entry e: expenses){
            if(!nazwyWydatkow.contains(e.name)){
                nazwyWydatkow.add(e.name);
                if(e.nameProduct.equals("Brak")){
                    tytulWydatku.add("Nazwa wydatku: \""+e.getName() +"\" Kategoria: \""+e.getCategory()+"\" Wartosc: \""+nf.format(e.getWartosc())+"\" Data: \""+sdf.format(e.date)+"\"");
                }else
                    tytulWydatku.add("Nazwa wydatku: \""+e.getName() +"\" Kategoria: \""+e.getCategory()+"\" Wartosc: \""+nf.format(e.getWartosc())+"\"");
                
            }
        }

        String[] lines = new String[expenses.size()+nazwyWydatkow.size()];

        int i = 0;
        for(int x=0; x<nazwyWydatkow.size();x++){
            lines[i++] = tytulWydatku.get(x);
           
            for (Entry e : expenses) {
                if(e.nameProduct.equals("Brak"))
                    continue;
                if(e.name.equals(nazwyWydatkow.get(x))){
                    lines[i++] = e.getString();
                }
            }
        }
        return lines;
    }

    public String[] getContributionsArray() {
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        addContributionsList(baza.pobierzPrzychody(HomeBudgetApp.getIdbudzetu()));
        baza.close();
        String[] lines = new String[contributions.size()];
        nf.setMinimumFractionDigits(2);
        int i = 0;
        for (Entry e : contributions) {            
            lines[i++] = "Nazwa przychodu: \""+e.getName() +"\" Kategoria: \""+e.getCategory()+"\" Wartosc: \""+nf.format(e.getWartosc())+"\" Data: \""+sdf.format(e.date)+"\"";
        }
        return lines;
    }
}
